package com.itxuhang.mvc.servlet;

import com.itxuhang.mvc.adapter.HandlerAdapter;
import com.itxuhang.mvc.mapping.HandlerMapping;
import com.itxuhang.spring.factory.DefaultListableBeanFactory;
import com.itxuhang.spring.reader.XmlBeanDefinitionReader;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

// 单个Servlet来分发所有的请求，所以该Servlet本身就不负责具体的请求处理了。
// 我们需要单独的处理类来处理请求 这个类可以称之为handler，该处理类和Servlet无关
public class DispatcherServlet extends AbstractServlet {
    private List<HandlerMapping> handlerMappings = new ArrayList<>();
    private List<HandlerAdapter> handlerAdapters = new ArrayList<>();

    private DefaultListableBeanFactory beanFactory ;

    @Override
    public void init(ServletConfig config) throws ServletException {
//        handlerMappings.add(new BeanNameUrlHandlerMapping());
//        handlerMappings.add(new SimpleUrlHandlerMapping());
//        handlerAdapters.add(new HttpRequestHandlerAdapter());
//        handlerAdapters.add(new SimpleControllerHandlerAdapter());

        String location = config.getInitParameter("contextConfigLocation");

        beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions(location);

        handlerMappings = beanFactory.getBeansByType(HandlerMapping.class);
        System.out.println(handlerMappings.size());
        handlerAdapters = beanFactory.getBeansByType(HandlerAdapter.class);
    }



    @Override
    public void dispatch(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        // 分发请求
        // 查找处理类handler
        Object handler = getHandler(request);
        if (handler == null){
            return ;
        }
        // 根据处理类的类型，查找对应的适配器
        HandlerAdapter ha = getHandlerAdapter(handler);
        if (ha == null){
            return ;
        }
        // 调用handler中的处理方法
        ha.handleRequest(handler,request,response);
    }

    private HandlerAdapter getHandlerAdapter(Object handler) {
        for (HandlerAdapter ha : handlerAdapters) {
            if (ha.supports(handler)){
                return ha;
            }
        }
        return null;
    }

    /*private void handleRequest(Object handler, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
//        if(handler instanceof HttpRequestHandler){
//            ((HttpRequestHandler)handler).handleRequest(request, response);
//        }else if (handler instanceof Controller){
//            ((SaveUserHandler)handler).handleRequest(request, response);
//        }
        HandlerAdapter
    }*/

    private Object getHandler(HttpServletRequest request){

        // 先一次性建立uri和handler的映射（可能用properties文件，也可能使用xml文件建立映射）
        // 再每次去该映射中，根据uri查找handler
        for (HandlerMapping hm : handlerMappings) {
            Object handler = hm.getHandler(request);
            if (handler != null){
                return handler ;
            }
        }

        return null;
    }

}
